package com.shundr.common;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.adapter.ChoiceAreaAdapter;
import com.shundr.common.dao.AreaDao;
import com.shundr.common.model.AreaInfo;
import com.shundr.common.util.JsonUtil;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.CListView;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.util.LLogger;
import com.shundr.frame.util.StringUtil;

public class ChoiceAreaActivity extends BaseActivity {
	
	private LinearLayout mLayoutChoiced;
	private TextView tvNotice;
	
	private CListView mListViewP;//省
	private CListView mListViewC;//市
	private CListView mListViewA;//区
	
	private List<AreaInfo> mListP = new ArrayList<AreaInfo>();
	private List<AreaInfo> mListC = new ArrayList<AreaInfo>();
	private List<AreaInfo> mListA = new ArrayList<AreaInfo>();
	
	private List<AreaInfo> selected = new ArrayList<AreaInfo>();
	
	private ChoiceAreaAdapter mAdapterP;
	private ChoiceAreaAdapter mAdapterC;
	private ChoiceAreaAdapter mAdapterA;
	
	private AreaDao mDao;
	
	private boolean isMul;
	private int max = 100;//最多可选数目
	private boolean isCountry = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choice_area);
		String title = getIntent().getStringExtra("title");
		if(!StringUtil.isNull(title)){
			setTitle(title);
		}
		isMul = getIntent().getBooleanExtra("is_mul", false);
		max = getIntent().getIntExtra("max", 100);
		isCountry = getIntent().getBooleanExtra("country", true);
		mDao = new AreaDao();
		mListP.addAll(mDao.getAreasByParent("00"));
		if(!isCountry && mListP.size() > 0){
			mListP.remove(0);
		}
		initView();
	}
	
	private void initView(){
		mLayoutChoiced = (LinearLayout)findViewById(R.id.layout_choiced);
		tvNotice = (TextView)findViewById(R.id.tv_notice);
		tvNotice.setText("请选择");
		
		mListViewP = (CListView)findViewById(R.id.lv_province);
		mListViewC = (CListView)findViewById(R.id.lv_city);
		mListViewA = (CListView)findViewById(R.id.lv_area);
		
		mAdapterP = new ChoiceAreaAdapter(mListP, mContext,false);
		mAdapterC = new ChoiceAreaAdapter(mListC, mContext,false);
		mAdapterA = new ChoiceAreaAdapter(mListA, mContext,true);
		
		mListViewP.setAdapter(mAdapterP);
		mListViewC.setAdapter(mAdapterC);
		mListViewA.setAdapter(mAdapterA);
		
		mListViewP.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AreaInfo info = mListP.get(position - 1);
				for(AreaInfo areaInfo : mListP){
					areaInfo.setSelected(false);
				}
				info.setSelected(true);
				mAdapterP.notifyDataSetChanged();
				if (StringUtil.isEqual(info.getCode(), LContext.nullStr)) {
					initList(info);
					mListC.clear();
					mAdapterC.notifyDataSetChanged();

					mListA.clear();
					mAdapterA.notifyDataSetChanged();
				} else {
					mListC.clear();
					mListC.addAll(mDao.getAreasByParent(info.getCode()));
					mAdapterC.notifyDataSetChanged();

					mListA.clear();
					mAdapterA.notifyDataSetChanged();
				}
			}
		});
		
		mListViewC.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AreaInfo info = mListC.get(position - 1);
				for(AreaInfo areaInfo : mListC){
					areaInfo.setSelected(false);
				}
				info.setSelected(true);
				mAdapterC.notifyDataSetChanged();
				if (StringUtil.isEqual(info.getCode(), info.getParentCode())) {
					initList(info);
					mListA.clear();
					mAdapterA.notifyDataSetChanged();
				} else {
					mListA.clear();
					mListA.addAll(mDao.getAreasByParent(info.getCode()));
					mAdapterA.notifyDataSetChanged();
				}
			}
		});
		
		mListViewA.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AreaInfo info = mListA.get(position - 1);
				for(AreaInfo areaInfo : mListA){
					areaInfo.setSelected(false);
				}
				info.setSelected(true);
				mAdapterA.notifyDataSetChanged();
				initList(info);
			}
		});
	}
	
	private void initList(AreaInfo info){
		boolean isContained = false;
		for(AreaInfo areaInfo : selected){
			if(StringUtil.isEqual(areaInfo.getCode(), info.getCode())){
				isContained = true;
			}
		}
		if(isContained){
			ToastHelper.toastShort(mContext, "该地区已选择");
			return;
		}
		if(selected.size() >= max){
			ToastHelper.toastShort(mContext, "最多只能选择" + max + "个");
			return;
		}
		if(!isMul){
			selected.clear();
		}
		selected.add(info);
		initSelectedView();
	}
	
	/**
	 * 动态添加布局
	 * initSelectedView(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	private void initSelectedView(){
		mLayoutChoiced.removeAllViews();
		for(int i = 0;i < selected.size();i++){
			AreaInfo area = selected.get(i);
			View view = LayoutInflater.from(mContext).inflate(R.layout.view_selected_area, null);
			TextView tvName = (TextView)view.findViewById(R.id.tv_value);
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int tag = (Integer)v.getTag();
					mLayoutChoiced.removeViewAt(tag);
					selected.remove(tag);
					initSelectedView();
				}
			});
			tvName.setText(area.getName());
			view.setTag(i);
			mLayoutChoiced.addView(view, i);
		}
		if(mLayoutChoiced.getChildCount() > 0){
			tvNotice.setVisibility(View.GONE);
		}else{
			tvNotice.setVisibility(View.VISIBLE);
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_confirm, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_confirm:
			if(selected.size() == 0){
				ToastHelper.toastShort(mContext, "请至少选择一个区域");
				return true;
			}
			List<AreaInfo> list = new ArrayList<AreaInfo>();
			if(selected.size() > 1){
				for(AreaInfo info : selected){
					if(!StringUtil.isEqual(info.getName(), "全国")){
						list.add(info);
					}
				}
			}else{
				list.addAll(selected);
			}
			String select = "";
			for(AreaInfo info : list){
				select = select + info.getName();
			}
			LLogger.e(select);
			Intent intent = new Intent();
			intent.putExtra("json", JsonUtil.toJson(list).toString());
			setResult(502,intent);
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}
